<?php 
/*
 module:		登录
 create_time:	2024-04-24 16:47:04
 author:		
 contact:		
*/

namespace app\api\service;
use app\api\model\Login;
use think\facade\Db;
use think\facade\Log;
use think\exception\ValidateException;
use utils\weixinpay\wechatpay;
use xhadmin\CommonService;

class LoginService extends CommonService {


	/*
 	* @Description  微信登录注册
 	*/
	public static function wxLogin($data,$user_openid){
		try{
            $wechat = new wechatpay();
            if(!empty($user_openid)){
                $is_complete = 1;
                $openid = $user_openid["openid"];
                $access_token = $user_openid["access_token"];
                $array = [
                    "openid"=>$openid,
                    "add_time"=>time(),
                    "is_complete"=>$is_complete
                ];

                //获取用户信息
                $user_wechat = Db::name("user_wechat")->where("openid",$openid)->find();
                $get_snsapi_userinfo = $wechat->get_snsapi_userinfo($access_token,$openid);
                if(!empty($get_snsapi_userinfo)){
                    $array["nickname"] = $get_snsapi_userinfo["nickname"];
                    $array["headimgurl"] = $get_snsapi_userinfo["headimgurl"];
                    $array["sex"] = $get_snsapi_userinfo["sex"];
                    $array["city"] = $get_snsapi_userinfo["city"];
                    $array["language"] = $get_snsapi_userinfo["language"];
                    $array["province"] = $get_snsapi_userinfo["province"];
                    $array["country"] = $get_snsapi_userinfo["country"];
                }
                if(empty($user_wechat)){
                    $uid = Db::name("user")->insertGetId([
                        "add_time"=>time(),
                    ]);


                    $array["uid"] = $uid;
                    //是否有上级订单号
                    if(isset($data["second"])){
                        $second = $data["second"];
                        $s_order = Db::name("order")->where("order_id",$second)->find();
                        if(!empty($s_order)){
                            Db::name("user_subordinate")->insert([
                                "uid"=>$uid,
                                "type"=>1,
                                "pid"=>$s_order["uid"],
                            ]);
                        }
                    }
                    Db::name("user_wechat")->insert($array);
                }else{
                    $uid = $user_wechat["uid"];
                    Db::name("user_wechat")->where("id",$user_wechat['id'])->update($array);
                }
                Db::name("user")->where("uid",$uid)->update(["last_time"=>time()]);
                //暂时access_token没用
                cache("access_token".$uid,$access_token);
                $res["uid"] = $uid;
            }else{
                throw new ValidateException('需重新授权');
            }

//            Db::name("user_wechat")->where("")

//			$res = Login::field($returnField)->where($where)->find();
		}catch(\Exception $e){
			abort(config('my.error_log_code'),$e->getMessage());
		}

		return checkData($res,false);
	}

    public static function phoneLogin($data)
    {

        try{
            $phone = $data["phone"];
            $pattern = '/^1[3456789]\d{9}$/';
            if (!preg_match($pattern, $phone)) {
                throw new ValidateException('请输入正确的手机号');
            }
            //判断验证码是否正确
            $uuid= $data["uuid"];
            $code = $data["code"];

            $phoneUuid = cache($uuid);
            if(empty($phoneUuid)){
                throw new ValidateException('验证码已过期，请重新获取');
            }
            if($phoneUuid != $code){
                throw new ValidateException('请输入正确的验证码');
            }
            //是否有用户
            $res = db("user")->where("phone",$phone)->field("uid")->find();
            if(empty($res)) {
                //没有用户要注册
                $uid = Db::name("user")->insertGetId([
                    "phone" => $phone,
                    "add_time" => time(),
                ]);
                $res["uid"] = $uid;
            }
        }catch(\Exception $e){
            abort(config('my.error_log_code'),$e->getMessage());
        }

        return checkData($res,false);
    }


}

